<?php

class Information extends FMIS_Controller
{
	public function __construct() {
		parent::__construct();
        $this->load->database();
        $this->load->model('information/Information_Topics_Model');
        //$this->load->helper('url');
	}

    public function view()
    {
        $topics = $this->Information_Topics_Model->getTopics();
        $topicTitles = array();
        $topicContents = array();
        $topicCounter = 0;
        foreach($topics as $topic)
        {
            $topicId = $topic['topic_id'];
            $topicName = htmlentities($topic['topic_name'], false);
            $topicContent = nl2br($topic['topic_content']);
            //$topicContent = preg_replace('#(<br />[\r\n]+){2}#', '</p><p>', $topicContent);
            //$topicContentHtml = htmlentities($topic['topic_content']);
            $form = form_open('information/update', array('id' => 'form'.$topicId));
            $topicContent = "<div class='tab-pane' id = 'topic".$topicId."'>".$form."<p name ='".$topic['topic_content']."'
            id = content".$topicId.">".$topicContent;
            $topicTitles[] = "<a href='#topic".$topicId."' data-toggle = 'tab'>".$topicName."</a>";
            $topicContentsAndId[] = array($topicContent, $topicId);
            $topicCounter++;
        }
        $data = array(
           'page_title'  => 'Information',
            'topicTitles' => $topicTitles,
            'topicContentsAndId' => $topicContentsAndId
        );
    	$this->setup_view('information/view', $data);
    }

    public function update()
    {
        $topicId = $this->input->post('id_holder');
        $topicContent = $this->input->post('textarea');
        //echo $topicId;
        $this->Information_Topics_Model->updateTopic($topicId, $topicContent);
        $this->view();
    }

    public function addTopic()
    {
        $topic_name =  $this->input->post('input_text_topicName');
        $this->Information_Topics_Model->addTopic($topic_name, ' ');
        $this->view();
    }
}

/* End of file information.php */
/* Location: ./application/controllers/information.php */